package com.yunshipei.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

import com.yunshipei.base.BaseActivity;
import com.yunshipei.base.BaseIntentBuilder;
import com.yunshipei.common.wedigt.YspTitleBar;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.MainExtraBean;
import com.yunshipei.ui.fragment.AboutFragment;
import com.yunshipei.ui.fragment.SettingsFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MultiActivity extends BaseActivity {

    private static final String EXTRA_MAIN_DATA = "extra.main.data";
    private static final String EXTRA_TARGET_CLAZZ_NAME = "extra.target.clazz.name";

    private String mTargetClazzName;
    private MainExtraBean mMainExtraBean;

    @Bind(R.id.et_title_bar)
    protected YspTitleBar mTitleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        mMainExtraBean = (MainExtraBean) intent.getSerializableExtra(EXTRA_MAIN_DATA);
        mTargetClazzName = intent.getStringExtra(EXTRA_TARGET_CLAZZ_NAME);
        initTarget();
    }

    private void initTarget() {
        if (!TextUtils.isEmpty(mTargetClazzName)) {
            if (mTargetClazzName.equals(SettingsFragment.class.getName())) {
                mTitleBar.setTitle(getString(R.string.setting));
                Fragment fragment = getSupportFragmentManager().findFragmentByTag(mTargetClazzName);
                if (fragment == null) {
                    fragment = SettingsFragment.newInstance(mMainExtraBean);
                    getSupportFragmentManager().beginTransaction().add(R.id.fl_target_part, fragment, mTargetClazzName).commit();
                }
            } else if (mTargetClazzName.equals(AboutFragment.class.getName())) {
                mTitleBar.setTitle(getString(R.string.about_info));
                Fragment fragment = getSupportFragmentManager().findFragmentByTag(mTargetClazzName);
                if (fragment == null) {
                    fragment = AboutFragment.newInstance(mMainExtraBean);
                    getSupportFragmentManager().beginTransaction().add(R.id.fl_target_part, fragment, mTargetClazzName).commit();
                }
            }
        }
    }

    public static class MultiActivityIntentBuilder extends BaseIntentBuilder {

        public MultiActivityIntentBuilder(Context context) {
            super(context);
        }

        public MultiActivityIntentBuilder setMainData(MainExtraBean mainData) {
            getIntent().putExtra(EXTRA_MAIN_DATA, mainData);
            return this;
        }

        public MultiActivityIntentBuilder setTargetPartClazzName(String clazzName) {
            getIntent().putExtra(EXTRA_TARGET_CLAZZ_NAME, clazzName);
            return this;
        }

        @Override
        protected Class<?> getTargetClazz() {
            return MultiActivity.class;
        }
    }
}
